package org.come.good;

import org.come.entity.Goodstable;
import org.come.mouslisten.GoodsMouslisten;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;

import com.tool.ModerateTask.Hero;
import com.tool.ModerateTask.Task;

//任务道具使用
public class TaskGood {

	/**
	 * 任务道具使用
	 */
	public static void gainTask(Goodstable goodstable){
		Task task=null;
		int[] tasks=TaskIdS(goodstable.getValue());
		for (int i = 0; i < tasks.length; i++) {
			task=Hero.getHero().getTaskId(tasks[i]);
			if (task!=null) {
				String mes = Agreement.getAgreement().TaskNAgreement("E"+task.getTaskId());
				SendMessageUntil.toServer(mes);
				goodstable.goodxh(1);
				GoodsMouslisten.gooduse(goodstable,1);
				break;
			}
		}
		if (task==null) {
			int taskid=accessTaskId(goodstable.getValue());
			String mes = Agreement.getAgreement().TaskNAgreement("L"+taskid);
			SendMessageUntil.toServer(mes);
		}
	}
	/**
	 * 获取全部的技能id
	 */
	public static int[] TaskIdS(String value){
		String mes = value.split("=")[1];
		String[] listTask = mes.split("\\|");
		int[] tasks=new int[listTask.length];
		for (int i = 0; i < listTask.length; i++) {
			String flag0 = listTask[i].split("\\&")[0];
			tasks[i] = Integer.parseInt(flag0);
		}
		return tasks;

	}
	/**
	 * 使用风灯 随机获得一个技能id的方法
	 */
	public static int accessTaskId(String value){
		String mes = value.split("=")[1];
		String[] listTask = mes.split("\\|");
		int index = (int)(Math.random()*100+1);
		int taskId = 0;//随机到的技能id
		int gl = 0;
		for (int i = 0; i < listTask.length; i++) {
			String flag0 = listTask[i].split("\\&")[0];
			String flag1 = listTask[i].split("\\&")[1];
			if(index>gl && index<=gl+Integer.parseInt(flag1)){
				taskId = Integer.parseInt(flag0);
			}
			gl += Integer.parseInt(flag1);
		}
		return taskId;
	}
}
